﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YJKSparseBreadth.ModelLayer;
using YJKSparseBreadth.Properties;
using YJKSparseBreadth.ViewLayer;
using YArchitech.Controls;

namespace YJKSparseBreadth.ControlLayer
{
	internal class ResultShowManger
	{
		internal ResultShowManger(List<AreaCheckResultBean> input, UIDocument inputUiDoc, IWin32Window inputRevitHandle, int inputAreaCount)
		{
			if (input == null)
			{
				this.m_lstAllResult = new List<AreaCheckResultBean>();
			}
			else
			{
				this.m_lstAllResult = input;
			}
			this.m_nAreaCount = inputAreaCount;
			this.m_RevitHandle = inputRevitHandle;
			this.m_useUiDoc = inputUiDoc;
		}

		internal void ShowWithWhile()
		{
			if (this.m_nAreaCount == 0)
			{
				YJKMessageBox.Show(Resources.Str_Message_NoneArea);
				return;
			}
			if (0 < this.GetWrongNumber(this.m_lstAllResult))
			{
				this.m_lstlstOrderedResult = this.OrderResultByLevel(this.m_lstAllResult);
				this.m_useForm = new ResultForm(this.m_lstlstOrderedResult, this.m_useUiDoc, this.m_RevitHandle);
				this.m_useForm.Show(this.m_RevitHandle);
				for (;;)
				{
					try
					{
						this.m_useUiDoc.Selection.PickElementsByRectangle();
						continue;
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						bool ifIsBackGroundEsc = this.m_useForm.IfIsBackGroundEsc;
						bool ifNeedContinue = this.m_useForm.IfNeedContinue;
						if (ifIsBackGroundEsc && !this.m_useForm.IfIsClose)
						{
							continue;
						}
						if (!ifIsBackGroundEsc || !this.m_useForm.IfIsClose)
						{
							if (ifNeedContinue)
							{
								continue;
							}
							this.m_useForm.Close();
						}
					}
					break;
				}
				this.m_useForm.AddTrustValue();
				return;
			}
			YJKMessageBox.Show(Resources.Str_Message_NoneError);
		}

		private int GetWrongNumber(List<AreaCheckResultBean> input)
		{
			return (from n in input
			where n.IfDesignWrong
			select n).Count<AreaCheckResultBean>();
		}

		private List<List<AreaCheckResultBean>> OrderResultByLevel(List<AreaCheckResultBean> input)
		{
			List<List<AreaCheckResultBean>> list = new List<List<AreaCheckResultBean>>();
			foreach (IEnumerable<AreaCheckResultBean> enumerable in from k in input
			group k by k.UseLevel.Id.IntegerValue)
			{
				list.Add(new List<AreaCheckResultBean>());
				foreach (AreaCheckResultBean item in enumerable)
				{
					list[list.Count - 1].Add(item);
				}
			}
			return list;
		}

		private List<AreaCheckResultBean> m_lstAllResult;

		private List<List<AreaCheckResultBean>> m_lstlstOrderedResult;

		private ResultForm m_useForm;

		private IWin32Window m_RevitHandle;

		private UIDocument m_useUiDoc;

		private int m_nAreaCount;
	}
}
